{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "import openai\n",
        "openai.api_key = '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'"
      ],
      "metadata": {
        "id": "IvYiuXzBwpW7"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "os.environ['OPENAI_API_KEY']='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'"
      ],
      "metadata": {
        "id": "1Bpkjpxiz0Em"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kpQ0liEMwRKg",
        "outputId": "a554719b-9ae9-4e22-f2d3-d47431c559e2"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting openai\n",
            "  Downloading openai-0.27.8-py3-none-any.whl (73 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m4.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai) (2.27.1)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai) (4.65.0)\n",
            "Collecting aiohttp (from openai)\n",
            "  Downloading aiohttp-3.8.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m35.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (1.26.15)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2022.12.7)\n",
            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2.0.12)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (3.4)\n",
            "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (23.1.0)\n",
            "Collecting multidict<7.0,>=4.5 (from aiohttp->openai)\n",
            "  Downloading multidict-6.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (114 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.5/114.5 kB\u001b[0m \u001b[31m10.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting async-timeout<5.0,>=4.0.0a3 (from aiohttp->openai)\n",
            "  Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)\n",
            "Collecting yarl<2.0,>=1.0 (from aiohttp->openai)\n",
            "  Downloading yarl-1.9.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (268 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m23.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting frozenlist>=1.1.1 (from aiohttp->openai)\n",
            "  Downloading frozenlist-1.3.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (149 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m149.6/149.6 kB\u001b[0m \u001b[31m13.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting aiosignal>=1.1.2 (from aiohttp->openai)\n",
            "  Downloading aiosignal-1.3.1-py3-none-any.whl (7.6 kB)\n",
            "Installing collected packages: multidict, frozenlist, async-timeout, yarl, aiosignal, aiohttp, openai\n",
            "Successfully installed aiohttp-3.8.4 aiosignal-1.3.1 async-timeout-4.0.2 frozenlist-1.3.3 multidict-6.0.4 openai-0.27.8 yarl-1.9.2\n",
            "\u001b[31mERROR: Could not open requirements file: [Errno 2] No such file or directory: 'rouge/requirements.txt'\u001b[0m\u001b[31m\n",
            "\u001b[0mLooking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting rouge-score\n",
            "  Downloading rouge_score-0.1.2.tar.gz (17 kB)\n",
            "  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "Requirement already satisfied: absl-py in /usr/local/lib/python3.10/dist-packages (from rouge-score) (1.4.0)\n",
            "Requirement already satisfied: nltk in /usr/local/lib/python3.10/dist-packages (from rouge-score) (3.8.1)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from rouge-score) (1.22.4)\n",
            "Requirement already satisfied: six>=1.14.0 in /usr/local/lib/python3.10/dist-packages (from rouge-score) (1.16.0)\n",
            "Requirement already satisfied: click in /usr/local/lib/python3.10/dist-packages (from nltk->rouge-score) (8.1.3)\n",
            "Requirement already satisfied: joblib in /usr/local/lib/python3.10/dist-packages (from nltk->rouge-score) (1.2.0)\n",
            "Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.10/dist-packages (from nltk->rouge-score) (2022.10.31)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from nltk->rouge-score) (4.65.0)\n",
            "Building wheels for collected packages: rouge-score\n",
            "  Building wheel for rouge-score (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for rouge-score: filename=rouge_score-0.1.2-py3-none-any.whl size=24934 sha256=ffe7f6d936323d2de30cd179858f226cd16d2a49a5f296180074ece12ab14203\n",
            "  Stored in directory: /root/.cache/pip/wheels/5f/dd/89/461065a73be61a532ff8599a28e9beef17985c9e9c31e541b4\n",
            "Successfully built rouge-score\n",
            "Installing collected packages: rouge-score\n",
            "Successfully installed rouge-score-0.1.2\n"
          ]
        }
      ],
      "source": [
        "!pip install openai\n",
        "!pip install -r rouge/requirements.txt\n",
        "!pip install rouge-score"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "import random\n",
        "\n",
        "prompts = [\n",
        "    \"What is the current price-to-earnings ratio of XYZ stock?\",\n",
        "    \"How does compound interest work?\",\n",
        "    \"What is a dividend yield?\",\n",
        "    \"Can you explain the concept of asset allocation?\",\n",
        "    \"What are the key factors affecting the forex market?\",\n",
        "    \"What is the difference between a bull market and a bear market?\",\n",
        "    \"How do you calculate return on investment (ROI)?\",\n",
        "    \"What is the significance of the Federal Reserve's interest rate decisions?\",\n",
        "    \"What are the main types of financial derivatives?\",\n",
        "    \"What is the role of an investment banker?\",\n",
        "    \"How does inflation impact purchasing power?\",\n",
        "    \"Can you explain the concept of capital gains tax?\",\n",
        "    \"What is the purpose of a stock exchange?\",\n",
        "    \"What are the risks associated with high-frequency trading?\",\n",
        "    \"How do you interpret a company's balance sheet?\",\n",
        "    \"What is the impact of supply and demand on stock prices?\",\n",
        "    \"Can you explain the concept of price elasticity of demand?\",\n",
        "    \"What are the key components of a mutual fund prospectus?\",\n",
        "    \"What is the difference between a traditional IRA and a Roth IRA?\",\n",
        "    \"How do you calculate the net present value (NPV) of an investment?\",\n",
        "    \"What is the significance of the debt-to-equity ratio?\",\n",
        "    \"Can you explain the concept of a credit default swap?\",\n",
        "    \"What are the main factors that drive economic growth?\",\n",
        "    \"How do you assess the risk profile of a bond?\",\n",
        "    \"What is the role of a financial analyst?\",\n",
        "    \"What are the main characteristics of a well-diversified investment portfolio?\",\n",
        "    \"What is the impact of fiscal policy on the economy?\",\n",
        "    \"Can you explain the concept of a stock option?\",\n",
        "    \"What are the key principles of value investing?\",\n",
        "    \"How do you evaluate the performance of a mutual fund?\",\n",
        "    \"What is the impact of exchange rates on international trade?\",\n",
        "    \"What are the different types of investment strategies?\",\n",
        "    \"Can you explain the concept of market capitalization?\",\n",
        "    \"What is the role of a credit rating agency?\",\n",
        "    \"How do you calculate the weighted average cost of capital (WACC)?\",\n",
        "    \"What are the main factors affecting bond yields?\",\n",
        "    \"What is the significance of the price-earnings-to-growth (PEG) ratio?\",\n",
        "    \"Can you explain the concept of a hedge fund?\",\n",
        "    \"What are the key characteristics of a well-managed company?\",\n",
        "    \"How do you analyze the profitability of a company?\",\n",
        "    \"What is the impact of a trade deficit on a country's economy?\",\n",
        "    \"What are the different types of financial markets?\",\n",
        "    \"Can you explain the concept of a stop-loss order?\",\n",
        "    \"What is the role of a central bank in monetary policy?\",\n",
        "    \"How do you assess the liquidity of an investment?\",\n",
        "    \"What is the impact of geopolitical events on the stock market?\",\n",
        "    \"What are the main factors affecting the price of commodities?\",\n",
        "    \"What is the significance of the efficient market hypothesis?\",\n",
        "    \"Can you explain the concept of a margin call?\",\n",
        "    \"What are the key steps in the financial planning process?\"\n",
        "]\n",
        "\n",
        "responses = [\n",
        "    \"The current price-to-earnings ratio of XYZ stock is 15.2.\",\n",
        "    \"Compound interest is the interest calculated on the initial principal as well as the accumulated interest from previous periods.\",\n",
        "    \"Dividend yield is a financial ratio that indicates the annual dividend income relative to the market price of the stock.\",\n",
        "    \"Asset allocation refers to the strategy of diversifying investments across different asset classes to manage risk and maximize returns.\",\n",
        "    \"The forex market is influenced by factors such as interest rates, economic indicators, geopolitical events, and market sentiment.\",\n",
        "    \"A bull market is characterized by rising prices and optimism, while a bear market is marked by falling prices and pessimism.\",\n",
        "    \"Return on investment (ROI) is calculated by dividing the net profit from an investment by the initial cost of the investment, expressed as a percentage.\",\n",
        "    \"The Federal Reserve's interest rate decisions have a significant impact on borrowing costs, inflation, and overall economic activity.\",\n",
        "    \"Financial derivatives are instruments whose value is derived from an underlying asset, index, or interest rate.\",\n",
        "    \"An investment banker assists companies in raising capital through issuing securities and provides advisory services for mergers and acquisitions.\",\n",
        "    \"Inflation erodes the purchasing power of money over time, reducing the value of goods and services.\",\n",
        "    \"Capital gains tax is a tax levied on the profits generated from the sale of assets such as stocks, bonds, or real estate.\",\n",
        "    \"A stock exchange provides a platform for buying and selling securities, facilitating liquidity and price discovery.\",\n",
        "    \"High-frequency trading involves executing trades at very high speeds using advanced algorithms, with potential risks such as market volatility and technical glitches.\",\n",
        "    \"A company's balance sheet provides information about its assets, liabilities, and shareholders' equity, giving insights into its financial position.\",\n",
        "    \"Stock prices are influenced by the forces of supply and demand, as well as factors such as company performance, industry trends, and market sentiment.\",\n",
        "    \"Price elasticity of demand measures the responsiveness of the quantity demanded to changes in price.\",\n",
        "    \"A mutual fund prospectus includes information about the fund's investment objectives, strategies, fees, risks, and historical performance.\",\n",
        "    \"A traditional IRA offers tax-deductible contributions and tax-deferred growth, while a Roth IRA provides tax-free withdrawals in retirement.\",\n",
        "    \"The net present value (NPV) of an investment is calculated by discounting the expected future cash flows to their present value and subtracting the initial investment.\",\n",
        "    \"The debt-to-equity ratio is a financial metric that compares a company's total debt to its shareholders' equity, indicating its leverage and financial risk.\",\n",
        "    \"A credit default swap is a financial derivative that allows investors to transfer the credit risk of a bond or loan to another party.\",\n",
        "    \"Economic growth is driven by factors such as investments, consumer spending, government policies, technological advancements, and global economic conditions.\",\n",
        "    \"The risk profile of a bond is assessed based on factors such as credit rating, maturity, coupon rate, and market conditions.\",\n",
        "    \"A financial analyst evaluates financial data, analyzes investment opportunities, and provides recommendations to individuals or organizations.\",\n",
        "    \"A well-diversified investment portfolio includes a mix of different asset classes, industries, and geographic regions to spread risk.\",\n",
        "    \"Fiscal policy refers to government decisions on spending, taxation, and borrowing, which influence economic activity and stability.\",\n",
        "    \"A stock option is a contract that gives the holder the right to buy or sell a specific stock at a predetermined price within a specified period.\",\n",
        "    \"Value investing focuses on identifying undervalued stocks based on fundamental analysis and long-term investment principles.\",\n",
        "    \"The performance of a mutual fund is evaluated based on factors such as returns, risk-adjusted measures, expense ratio, and fund manager's expertise.\",\n",
        "    \"Exchange rates impact international trade by affecting the relative prices of goods and services between countries.\",\n",
        "    \"Different investment strategies include growth investing, value investing, income investing, momentum investing, and contrarian investing.\",\n",
        "    \"Market capitalization is the total value of a company's outstanding shares, calculated by multiplying the share price by the number of shares.\",\n",
        "    \"A credit rating agency assesses the creditworthiness of borrowers and assigns credit ratings that reflect their ability to repay debt.\",\n",
        "    \"The weighted average cost of capital (WACC) is the average cost of financing a company's operations, incorporating the costs of debt and equity.\",\n",
        "    \"Bond yields are influenced by factors such as interest rates, credit quality, maturity, market demand, and macroeconomic conditions.\",\n",
        "    \"The price-earnings-to-growth (PEG) ratio compares a company's price-to-earnings ratio with its expected earnings growth rate, providing insights into its valuation.\",\n",
        "    \"A hedge fund is an investment fund that pools capital from accredited investors and employs a variety of investment strategies to generate returns.\",\n",
        "    \"Well-managed companies demonstrate strong corporate governance, efficient operations, sound financial management, and strategic decision-making.\",\n",
        "    \"The profitability of a company is analyzed through metrics such as profit margin, return on equity, return on assets, and earnings per share.\",\n",
        "    \"A trade deficit occurs when a country's imports exceed its exports, impacting its balance of trade and potentially its currency value.\",\n",
        "    \"Financial markets include stock markets, bond markets, commodity markets, currency markets, derivatives markets, and money markets.\",\n",
        "    \"A stop-loss order is a type of order placed with a broker to automatically sell a security if its price falls below a specified level, limiting potential losses.\",\n",
        "    \"A central bank implements monetary policy, regulates banks, manages foreign exchange reserves, and maintains stability in the financial system.\",\n",
        "    \"The liquidity of an investment refers to its ability to be converted into cash quickly and with minimal impact on its price.\",\n",
        "    \"Geopolitical events such as political instability, wars, natural disasters, and international conflicts can affect investor confidence and market conditions.\",\n",
        "    \"The price of commodities is influenced by factors such as supply and demand dynamics, global economic conditions, weather patterns, and geopolitical tensions.\",\n",
        "    \"The efficient market hypothesis suggests that financial markets incorporate all available information and reflect it in asset prices, making it difficult to consistently outperform the market.\",\n",
        "    \"A margin call is a demand by a broker for an investor to deposit additional funds or securities to cover potential losses in a margin account.\",\n",
        "    \"The financial planning process involves setting financial goals, assessing current financial status, creating a budget, implementing strategies, and monitoring progress.\"\n",
        "]\n",
        "\n",
        "data = {'prompt': prompts[:50], 'completion': responses[:50]}\n",
        "df = pd.DataFrame(data)\n",
        "\n",
        "df.to_csv('financial_data.csv', index=False)\n"
      ],
      "metadata": {
        "id": "twAn1AxDxDhj"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!openai tools fine_tunes.prepare_data -f /content/financial_data.csv"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PSyOtD9vxlT_",
        "outputId": "a03a8f6f-f780-40ad-95e2-903eeaa46509"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Analyzing...\n",
            "\n",
            "- Based on your file extension, your file is formatted as a CSV file\n",
            "- Your file contains 50 prompt-completion pairs. In general, we recommend having at least a few hundred examples. We've found that performance tends to linearly increase for every doubling of the number of examples\n",
            "- All prompts end with suffix `?`\n",
            "- All completions end with suffix `.`\n",
            "  WARNING: Some of your completions contain the suffix `.` more than once. We suggest that you review your completions and add a unique ending\n",
            "- The completion should start with a whitespace character (` `). This tends to produce better results due to the tokenization we use. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more details\n",
            "\n",
            "Based on the analysis we will perform the following actions:\n",
            "- [Necessary] Your format `CSV` will be converted to `JSONL`\n",
            "- [Recommended] Add a whitespace character to the beginning of the completion [Y/n]: y\n",
            "\n",
            "\n",
            "Your data will be written to a new JSONL file. Proceed [Y/n]: y\n",
            "\n",
            "Wrote modified file to `/content/financial_data_prepared.jsonl`\n",
            "Feel free to take a look!\n",
            "\n",
            "Now use that file when fine-tuning:\n",
            "> openai api fine_tunes.create -t \"/content/financial_data_prepared.jsonl\"\n",
            "\n",
            "After you’ve fine-tuned a model, remember that your prompt has to end with the indicator string `?` for the model to start generating completions, rather than continuing with the prompt. Make sure to include `stop=[\".\"]` so that the generated texts ends at the expected place.\n",
            "Once your model starts training, it'll approximately take 3.13 minutes to train a `curie` model, and less for `ada` and `babbage`. Queue will approximately take half an hour per job ahead of you.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!openai api fine_tunes.create -t \"/content/financial_data_prepared.jsonl\" -m davinci"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WOP1HIK8xnET",
        "outputId": "5a23a6cc-7065-45e8-e6db-c856b3af37bf"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Found potentially duplicated files with name 'financial_data_prepared.jsonl', purpose 'fine-tune' and size 11069 bytes\n",
            "file-d1TL4cryqnsq0ZSdWMm6X8gb\n",
            "Enter file ID to reuse an already uploaded file, or an empty string to upload this file anyway: \n",
            "Upload progress: 100% 11.1k/11.1k [00:00<00:00, 12.6Mit/s]\n",
            "Uploaded file from /content/financial_data_prepared.jsonl: file-krrIrfRkH58upMRnYPyzvKvb\n",
            "Created fine-tune: ft-u6tXPdrklhDfN1nPpFrJv91Z\n",
            "Streaming events until fine-tuning is complete...\n",
            "\n",
            "(Ctrl-C will interrupt the stream, but not cancel the fine-tune)\n",
            "[2023-06-17 15:48:43] Created fine-tune: ft-u6tXPdrklhDfN1nPpFrJv91Z\n",
            "\n",
            "\n",
            "Stream interrupted (client disconnected).\n",
            "To resume the stream, run:\n",
            "\n",
            "  openai api fine_tunes.follow -i ft-u6tXPdrklhDfN1nPpFrJv91Z\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!openai api fine_tunes.follow -i ft-u6tXPdrklhDfN1nPpFrJv91Z"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hGhAIiFUDmyc",
        "outputId": "996d05c9-0f22-4e9c-ea38-c7c5e112f6fb"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[2023-06-17 15:48:43] Created fine-tune: ft-u6tXPdrklhDfN1nPpFrJv91Z\n",
            "[2023-06-17 15:50:50] Fine-tune costs $0.21\n",
            "[2023-06-17 15:50:50] Fine-tune enqueued. Queue number: 0\n",
            "[2023-06-17 15:50:52] Fine-tune started\n",
            "\n",
            "Stream interrupted (client disconnected).\n",
            "To resume the stream, run:\n",
            "\n",
            "  openai api fine_tunes.follow -i ft-u6tXPdrklhDfN1nPpFrJv91Z\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!openai api fine_tunes.get -i ft-u6tXPdrklhDfN1nPpFrJv91Z"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QevzEIoY0n1A",
        "outputId": "c46ec15a-1403-4b3e-81f8-4eb8d11dc11a"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\n",
            "  \"object\": \"fine-tune\",\n",
            "  \"id\": \"ft-u6tXPdrklhDfN1nPpFrJv91Z\",\n",
            "  \"hyperparams\": {\n",
            "    \"n_epochs\": 4,\n",
            "    \"batch_size\": 1,\n",
            "    \"prompt_loss_weight\": 0.01,\n",
            "    \"learning_rate_multiplier\": 0.1\n",
            "  },\n",
            "  \"organization_id\": \"org-kyT7ncCq3gcAOv1pqp1cbOfG\",\n",
            "  \"model\": \"davinci\",\n",
            "  \"training_files\": [\n",
            "    {\n",
            "      \"object\": \"file\",\n",
            "      \"id\": \"file-krrIrfRkH58upMRnYPyzvKvb\",\n",
            "      \"purpose\": \"fine-tune\",\n",
            "      \"filename\": \"/content/financial_data_prepared.jsonl\",\n",
            "      \"bytes\": 11069,\n",
            "      \"created_at\": 1687016923,\n",
            "      \"status\": \"processed\",\n",
            "      \"status_details\": null\n",
            "    }\n",
            "  ],\n",
            "  \"validation_files\": [],\n",
            "  \"result_files\": [\n",
            "    {\n",
            "      \"object\": \"file\",\n",
            "      \"id\": \"file-y9LZgpjAc1DGmLT1qJsupDkD\",\n",
            "      \"purpose\": \"fine-tune-results\",\n",
            "      \"filename\": \"compiled_results.csv\",\n",
            "      \"bytes\": 9509,\n",
            "      \"created_at\": 1687017319,\n",
            "      \"status\": \"processed\",\n",
            "      \"status_details\": null\n",
            "    }\n",
            "  ],\n",
            "  \"created_at\": 1687016923,\n",
            "  \"updated_at\": 1687017319,\n",
            "  \"status\": \"succeeded\",\n",
            "  \"fine_tuned_model\": \"davinci:ft-think-in-bytes-2023-06-17-15-55-17\",\n",
            "  \"events\": [\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Created fine-tune: ft-u6tXPdrklhDfN1nPpFrJv91Z\",\n",
            "      \"created_at\": 1687016923\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Fine-tune costs $0.21\",\n",
            "      \"created_at\": 1687017050\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Fine-tune enqueued. Queue number: 0\",\n",
            "      \"created_at\": 1687017050\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Fine-tune started\",\n",
            "      \"created_at\": 1687017052\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Completed epoch 1/4\",\n",
            "      \"created_at\": 1687017238\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Completed epoch 2/4\",\n",
            "      \"created_at\": 1687017253\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Completed epoch 3/4\",\n",
            "      \"created_at\": 1687017269\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Completed epoch 4/4\",\n",
            "      \"created_at\": 1687017284\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Uploaded model: davinci:ft-think-in-bytes-2023-06-17-15-55-17\",\n",
            "      \"created_at\": 1687017318\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Uploaded result file: file-y9LZgpjAc1DGmLT1qJsupDkD\",\n",
            "      \"created_at\": 1687017319\n",
            "    },\n",
            "    {\n",
            "      \"object\": \"fine-tune-event\",\n",
            "      \"level\": \"info\",\n",
            "      \"message\": \"Fine-tune succeeded\",\n",
            "      \"created_at\": 1687017319\n",
            "    }\n",
            "  ]\n",
            "}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import openai\n",
        "response = openai.Completion.create(\n",
        "    model='davinci:ft-think-in-bytes-2023-06-13-22-27-38',\n",
        "    prompt='Discuss the concept of a margin call?')"
      ],
      "metadata": {
        "id": "fqt7nFjjzbx5"
      },
      "execution_count": 13,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response"
      ],
      "metadata": {
        "id": "-GSChano0gF_",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "1f0b4d8e-6b64-4bb6-c06b-2cbf600475cd"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<OpenAIObject text_completion id=cmpl-7SSX9ZMt6ueKSyTp4DB1711EJAsYI at 0x7fae0886f150> JSON: {\n",
              "  \"id\": \"cmpl-7SSX9ZMt6ueKSyTp4DB1711EJAsYI\",\n",
              "  \"object\": \"text_completion\",\n",
              "  \"created\": 1687017223,\n",
              "  \"model\": \"davinci:ft-think-in-bytes-2023-06-13-22-27-38\",\n",
              "  \"choices\": [\n",
              "    {\n",
              "      \"text\": \" A margin call is a demand by a broker for an investor to deposit additional funds\",\n",
              "      \"index\": 0,\n",
              "      \"logprobs\": null,\n",
              "      \"finish_reason\": \"length\"\n",
              "    }\n",
              "  ],\n",
              "  \"usage\": {\n",
              "    \"prompt_tokens\": 8,\n",
              "    \"completion_tokens\": 16,\n",
              "    \"total_tokens\": 24\n",
              "  }\n",
              "}"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Call custom Model - Rogue score"
      ],
      "metadata": {
        "id": "GJquLW9qc9b4"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "predicted = response['choices'][0]['text']\n",
        "ground_truth = 'A margin call is a demand by a broker for an investor to deposit additional funds or securities to cover potential losses in a margin account.'"
      ],
      "metadata": {
        "id": "rn28LVaFbZcP"
      },
      "execution_count": 15,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from rouge_score import rouge_scorer\n",
        "\n",
        "scorer = rouge_scorer.RougeScorer(['rouge1', 'rougeL'], use_stemmer=True)\n",
        "scores = scorer.score(predicted,ground_truth)\n",
        "print(scores)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8473QAJPb4Wh",
        "outputId": "8c3a8579-e49d-4522-c24f-7a73aa4ccfbb"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'rouge1': Score(precision=0.6153846153846154, recall=1.0, fmeasure=0.761904761904762), 'rougeL': Score(precision=0.6153846153846154, recall=1.0, fmeasure=0.761904761904762)}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from nltk.translate.bleu_score import sentence_bleu\n",
        "\n",
        "print('BLEU score -> {}'.format(sentence_bleu(ground_truth.split(), predicted.split())))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MDTGuND1CXS7",
        "outputId": "642b3364-4d1a-46f2-b09e-7c0879371657"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "BLEU score -> 1.1988328686372911e-231\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.10/dist-packages/nltk/translate/bleu_score.py:552: UserWarning: \n",
            "The hypothesis contains 0 counts of 2-gram overlaps.\n",
            "Therefore the BLEU score evaluates to 0, independently of\n",
            "how many N-gram overlaps of lower order it contains.\n",
            "Consider using lower n-gram order or use SmoothingFunction()\n",
            "  warnings.warn(_msg)\n",
            "/usr/local/lib/python3.10/dist-packages/nltk/translate/bleu_score.py:552: UserWarning: \n",
            "The hypothesis contains 0 counts of 3-gram overlaps.\n",
            "Therefore the BLEU score evaluates to 0, independently of\n",
            "how many N-gram overlaps of lower order it contains.\n",
            "Consider using lower n-gram order or use SmoothingFunction()\n",
            "  warnings.warn(_msg)\n",
            "/usr/local/lib/python3.10/dist-packages/nltk/translate/bleu_score.py:552: UserWarning: \n",
            "The hypothesis contains 0 counts of 4-gram overlaps.\n",
            "Therefore the BLEU score evaluates to 0, independently of\n",
            "how many N-gram overlaps of lower order it contains.\n",
            "Consider using lower n-gram order or use SmoothingFunction()\n",
            "  warnings.warn(_msg)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Call non-finetuned model - Rogue score"
      ],
      "metadata": {
        "id": "5wSnhp93dH4Q"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import openai\n",
        "response = openai.Completion.create(\n",
        "    model='text-davinci-003',\n",
        "    prompt='Discuss the concept of a margin call?')"
      ],
      "metadata": {
        "id": "yPcLJy0qcbDq"
      },
      "execution_count": 18,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "predicted = response['choices'][0]['text']\n",
        "print(predicted)\n",
        "ground_truth = 'A margin call is a demand by a broker for an investor to deposit additional funds or securities to cover potential losses in a margin account.'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-0gd-wlYcx0h",
        "outputId": "d6149d8a-86ad-451e-dc8c-fa359921e20e"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "A margin call is a demand by a brokerage firm to its investor to\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from rouge_score import rouge_scorer\n",
        "\n",
        "scorer = rouge_scorer.RougeScorer(['rouge1', 'rougeL'], use_stemmer=True)\n",
        "scores = scorer.score(predicted,ground_truth)\n",
        "print(scores)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7HGaSp-bc2G-",
        "outputId": "53ea7234-c056-4e06-8d90-7a4b17280fa9"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'rouge1': Score(precision=0.4230769230769231, recall=0.7857142857142857, fmeasure=0.55), 'rougeL': Score(precision=0.38461538461538464, recall=0.7142857142857143, fmeasure=0.5)}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from nltk.translate.bleu_score import sentence_bleu\n",
        "\n",
        "print('BLEU score -> {}'.format(sentence_bleu(ground_truth.split(), predicted.split())))"
      ],
      "metadata": {
        "id": "BCaUXpX4c4wv",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "bfa4a58d-7190-4ad1-a80b-a0ebf85922a6"
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "BLEU score -> 1.2395288183339461e-231\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "JFD6_DuZEJh-"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}